package com.chart.web.controller.chart.sta;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.domian.ChartData;
import com.chart.chart.domian.ChartLegendData;
import com.chart.chart.domian.vo.ChartXData;
import com.chart.chart.domian.vo.ChartXDataVo;
import com.chart.chart.service.IChartConfigsService;
import com.chart.common.core.controller.BaseController;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.ExcelDataUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 柱状图
 *
 * @author chart
 */
@RestController
@RequestMapping("/bar/chart")
public class BarChartController extends BaseController
{


    @Autowired
    private IChartConfigsService chartConfigsService;


    //堆叠柱状图
    @PostMapping("/stackedColumn/importData")
    public AjaxResult importData(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);
            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> fistName = new ArrayList<>();
            List<String> nameList = getdata.get(0);
            for (int i = 0; i < nameList.size(); i++) {
                String s = nameList.get(i);
                if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                    fistName.add(s);
                }
            }

            List<String> roomName = new ArrayList<>();



            List<List<String>> list = new ArrayList<>();
            list.add(fistName);

            List<Map> maps = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<String> list1 = getdata.get(i);
                if (!CollectionUtils.isEmpty(list1)) {
                    List<String> data = new ArrayList<>();
                    for (int i1 = 0; i1 < list1.size(); i1++) {
                        String s = list1.get(i1);
                        if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                            data.add(s);
                        }
                    }

                    if (data.size()==1) {
                        if (i!=1) {
                            Map map = new HashMap<>();
                            map.put("source",list);
                            maps.add(map);
                            list=new ArrayList<>();
                            list.add(fistName);
                        }
                        roomName.add(data.get(0));
                    }else {
                        list.add(data);
                        if (i==getdata.size()-1) {
                            Map map = new HashMap<>();
                            map.put("source",list);
                            maps.add(map);
                        }
                    }
                }

            }

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject series1 = series.get(0);
            List<JSONObject> sDatas = new ArrayList<>();


//            fistName.remove(0);
            if (roomName.size() < 1) {
                for (int i = 1; i < fistName.size(); i++) {
                    JSONObject object = new JSONObject();
                    object.putAll(series1);
                    object.put("name", fistName.get(i));
                    sDatas.add(object);
                }
            } else {
                for (int i = 0; i < roomName.size(); i++) {
//                Integer indexNum = roomNameIndex.get(i);
                    for (Integer integer = 1; integer < fistName.size(); integer++) {
                        JSONObject object = new JSONObject();
                        object.putAll(series1);
                        object.put("stack", roomName.get(i));
                        object.put("datasetIndex", i);
                        object.put("name", fistName.get(integer));
                        sDatas.add(object);
                    }
                }
            }


            jsonObject.put("dataset", maps);
            jsonObject.put("series", sDatas);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            List<ChartLegendData> chartLegendData = new ArrayList<>();
            for (int i = 1; i < fistName.size(); i++) {
                ChartLegendData data = new ChartLegendData();
                data.setName(fistName.get(i));
                chartLegendData.add(data);
            }
            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);
            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }




    //带包络线的分组堆叠柱状图
    @PostMapping("/stackedColumnSmoothed/importData")
    public AjaxResult stackedColumnSmoothed(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> fistName = new ArrayList<>();
            List<String> nameList = getdata.get(0);
            for (int i = 0; i < nameList.size(); i++) {
                String s = nameList.get(i);
                if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                    fistName.add(s);
                }
            }

            List<String> roomName = getdata.get(1);

            List<Integer> roomNameIndex = new ArrayList<>();
            int k = 0;

            List<ChartXData> xDatas = new ArrayList<>();


            List<List<String>> list = new ArrayList<>();
            for (int i = 2; i < getdata.size(); i++) {
                if (getdata.get(i).size() == 1) {
                    list.add(0, fistName);
                    ChartXData chartXData = new ChartXData();
                    chartXData.setSource(list);
                    xDatas.add(chartXData);
                    list = new ArrayList<>();
                    roomNameIndex.add(k);
                    roomName.add(getdata.get(i).get(0));
                    k = 0;
                } else {
                    k++;
                    List<String> strings=new ArrayList<>();
                    List<String> oldList = getdata.get(i);
                    for (int i1 = 0; i1 < oldList.size(); i1++) {
                        String s = oldList.get(i1);
                        if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                            strings.add(s);
                        }
                    }
                    list.add(strings);
                }
                if (i == getdata.size() - 1) {
                    k++;
                    list.add(0, fistName);
                    ChartXData chartXData = new ChartXData();
                    chartXData.setSource(list);
                    xDatas.add(chartXData);
                    roomNameIndex.add(k);
                }
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject series1 = series.get(0);
            List<JSONObject> sDatas = new ArrayList<>();


            fistName.remove(0);
            if (roomName.size() < 1) {
                for (int i = 0; i < fistName.size(); i++) {
                    JSONObject object = new JSONObject();
                    object.putAll(series1);
                    object.put("stack", roomName.get(i));
                    object.put("name", fistName.get(i));
                    sDatas.add(object);
                }
            } else {
                for (int i = 0; i < roomNameIndex.size(); i++) {
//                Integer indexNum = roomNameIndex.get(i);
                    for (Integer integer = 0; integer < fistName.size(); integer++) {
                        JSONObject object = new JSONObject();
                        object.putAll(series1);
                        object.put("stack", roomName.get(i));
                        object.put("datasetIndex", i);
                        object.put("name", fistName.get(integer));
                        sDatas.add(object);
                    }
                }
            }


            ChartData chartData = new ChartData();
            chartData.setDataset(xDatas);

            jsonObject.put("dataset", xDatas);
            jsonObject.put("series", sDatas);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            List<ChartLegendData> chartLegendData = new ArrayList<>();
            for (int i = 0; i < fistName.size(); i++) {
                ChartLegendData data = new ChartLegendData();
                data.setName(fistName.get(i));
                chartLegendData.add(data);
            }
            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }


    //水平条形柱状图
    @PostMapping("/horizontalBar/importData")
    public AjaxResult horizontalBar(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<Map> roomName = new ArrayList<>();

            for (int i = 1; i < getdata.size(); i++) {
                List<String> strings = getdata.get(i);
                HashMap<String, String> map = new HashMap<>();
                String name = strings.get(0);
                if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(name) || !name.equals("")) {
                    map.put("name", strings.get(0));
                }
                roomName.add(map);
            }

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            legend.put("data",roomName);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject json = series.get(0);
            ArrayList<JSONObject> list = new ArrayList<>();

            for (int i = 0; i < roomName.size(); i++) {
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.putAll(json);
                jsonObject1.put("name",roomName.get(i).get("name"));
                List<String> list1 = getdata.get(i+1);
                ArrayList<String> newList = new ArrayList<>();
                for (int i1 = 0; i1 < list1.size(); i1++) {
                    String s = list1.get(i1);
                    if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                        newList.add(s);
                    }
                }
                if (newList.size() >= 2) {
                    List<String> sublistExcludingFirstTwo = newList.subList(1, list1.size());
                    jsonObject1.put("data",sublistExcludingFirstTwo);
                }
                list.add(jsonObject1);
            }

            jsonObject.put("legend",legend);
            jsonObject.put("series",list);

            ArrayList<String> strings = new ArrayList<>();
            List<String> list1 = getdata.get(0);
            for (int i = 1; i < list1.size(); i++) {
                String s = list1.get(i);
                if (ObjectUtils.isNotEmpty(s) && !s.equals(s)) {
                    strings.add(s);
                }
            }
            JSONObject yAxis = (JSONObject) jsonObject.get("yAxis");
            yAxis.put("data",strings);
            jsonObject.put("yAxis",yAxis);


            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }


    //基础柱状图
    @PostMapping("/basic/importData")
    public AjaxResult basic(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<Map> roomName = new ArrayList<>();


            List<String> strings = getdata.get(1);
            HashMap<String, String> map = new HashMap<>();
            map.put("name", strings.get(0));
            roomName.add(map);

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            legend.put("data",roomName);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");

            ArrayList<JSONObject> list = new ArrayList<>();
            for (int i = 0; i < series.size(); i++) {
                JSONObject o1 = series.get(i);
                o1.put("name",roomName.get(i).get("name"));
                List<String> list1 =getdata.get(i+1);
                ArrayList<String> newList = new ArrayList<>();
                for (int i1 = 1; i1 < list1.size(); i1++) {
                    String s = list1.get(i1);
                    if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                        newList.add(s);
                    }
                }
                o1.put("data",newList);
                list.add(o1);
            }

            JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
            List<String> strings1 = getdata.get(0);
            strings1.remove(0);

            xAxis.put("data",strings1);

            jsonObject.put("xAxis",xAxis);
            jsonObject.put("legend",legend);
            jsonObject.put("series",list);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }


    //带背景色的柱状图
    @PostMapping("/background/importData")
    public AjaxResult background(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<Map> roomName = new ArrayList<>();


            List<String> strings = getdata.get(1);
            HashMap<String, String> map = new HashMap<>();
            map.put("name", strings.get(0));
            roomName.add(map);

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            legend.put("data",roomName);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");

            ArrayList<JSONObject> list = new ArrayList<>();
            for (int i = 0; i < series.size(); i++) {
                JSONObject o1 = series.get(i);
                o1.put("name",roomName.get(i).get("name"));
                List<String> list1 =getdata.get(i+1);
                ArrayList<String> newList = new ArrayList<>();
                for (int i1 = 1; i1 < list1.size(); i1++) {
                    String s = list1.get(i1);
                    if (ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                        newList.add(s);
                    }
                }
                o1.put("data",newList);
                list.add(o1);
            }
            JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
            List<String> strings1 = getdata.get(0);
            strings1.remove(0);

            xAxis.put("data",strings1);

            jsonObject.put("xAxis",xAxis);
            jsonObject.put("legend",legend);
            jsonObject.put("series",list);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }



    //归一化堆叠柱状图
    @PostMapping("/singleStackedColumn/importData")
    public AjaxResult multiLineSmoothLine(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xlist = new ArrayList<>();
            List<String> firstName = new ArrayList<>();
            List<List<Double>> dataList = new ArrayList<>();
            for (int i = 0; i < getdata.size(); i++) {
                List<String> rowData = getdata.get(i);
                ArrayList<String> newList = new ArrayList<>();
                double snum=0;
                for (int i1 = 0; i1 < rowData.size(); i1++) {
                    String s = rowData.get(i1);
                    if (!org.springframework.util.ObjectUtils.isEmpty(s) && !s.equals("")) {
                        newList.add(s);
                        if (i != 0) {
                            if (i1 !=0) {
                                snum += Double.parseDouble(s);
                            }
                        }
                    }
                }

                List<Double> doubles = new ArrayList<>();
                if (!CollectionUtils.isEmpty(newList)) {
                    if (i == 0) {
                        firstName.addAll(newList);
                    } else {
                        for (int i1 = 0; i1 < newList.size(); i1++) {
                            String s = newList.get(i1);
                            if (i1==0) {
                                xlist.add(s);
                            }else {
                                double v = Double.parseDouble(s);
                                doubles.add(v/snum);
                            }
                        }
                    }
                    if (!CollectionUtils.isEmpty(doubles)) {
                        dataList.add(doubles);
                    }

                }
            }

            firstName.remove(0);
            List<List<Double>> datass = new ArrayList<>();
            for (int i = 0; i < firstName.size(); i++) {
                List<Double> datas = new ArrayList<>();
                datass.add(datas);
            }


            for (int i = 0; i < dataList.size(); i++) {
                List<Double> doubles = dataList.get(i);
                for (int i1 = 0; i1 < doubles.size(); i1++) {
                    List<Double> doubles1 = datass.get(i1);
                    doubles1.add(doubles.get(i1));
                }
            }



            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

            JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
//        xlist.remove(0);
            xAxis.put("data",xlist);
            jsonObject.put("xAxis",xAxis);

            JSONObject legend = (JSONObject) jsonObject.get("legend");

            List<JSONObject> newList =new ArrayList<>();
            List<Map> maps =new ArrayList<>();
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(0);
            for (int i = 0; i < firstName.size(); i++) {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);

                jsonObject2.put("name",firstName.get(i));
                Map<String, String> map = new HashMap<>();
                map.put("name",firstName.get(i));
                maps.add(map);
                jsonObject2.put("data",datass.get(i));
                newList.add(jsonObject2);
            }
            legend.put("data",maps);
            jsonObject.put("legend",legend);

            jsonObject.put("series",newList);


            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally{
            workbook.close();
        }
    }

}
